(function ($, _) {
    window.defaultInit = {
        data:{},
        init:function () {
            //空实现
        }
    };
    window.interfaceDoc = {
        data: {},
        init: function () {
            window.interfaceDoc.initData();
        },
        initData:function () {
            var that = this;
            $.getJSON("/data/Doc.json", function (result) {
                that.data.docs = result;
                that.initMenu();
                that.initClick();
            });
        },
        initMenu: function () {
            var leftMenuT = this.loadTemplate('t_left_menu');
            var leftMenuE = leftMenuT(this.getInterfaces());
            $('#left_menu').append(leftMenuE);
            $('#jstree_vehicle').jstree({
                "types": {
                    "default": {
                        "icon": false
                    }
                },
                "plugins": ["unique", "wholerow", "types", "changed"]
            });
        },
        initClick: function () {
            var that = this;
            $("#jstree_vehicle").on("changed.jstree", function (e, data) {
                if (data.changed.selected.length != 0) {
                    if (!$('#' + data.changed.selected[0] + '').attr('data-flag')) {
                        //对于父节点
                        $.each(that.data.docs, function (index, doc) {
                            if (doc.uri == data.changed.selected[0]) {
                                that.loadModule(doc);
                            }
                        });
                    } else {
                        //对于子节点
                        $.each(that.data.docs, function (index, doc) {
                            //找到module
                            if (doc.uri == $('#' + data.changed.selected[0] + '').attr('data-flag')) {
                                //找到interface文档
                                $.each(doc.customInterfaceDocs, function (index, interfaceDoc) {
                                    if (data.changed.selected[0] == interfaceDoc.name) {
                                        console.log(interfaceDoc);
                                        that.loadIterface(interfaceDoc);
                                    }
                                });
                            }
                        });
                    }
                }
            });
        },
        getInterfaces: function () {
            var that = this;
            return that.data.docs;
        },
        loadModule: function (doc) {
            var moduleT = this.loadTemplate('t_module_doc');
            var moduleE = moduleT(doc);
            $('#class_details').empty()
            $('#class_details').append(moduleE);
        },
        loadIterface: function (doc) {
            var interfaceT = this.loadTemplate('t_interface_doc');
            var interfaceE = interfaceT(doc);
            $('#class_details').empty()
            $('#class_details').append(interfaceE);
            this.initBtn();
        },
        loadTemplate: function (tplID) {
            var tpl = $.ajax({url: './template/' + tplID + '.html', async: false});
            if (tpl.status == 200) {
                $('#class_details').append(tpl.responseText);
                return _.template($('#'+tplID).html())
            } else {
                return _.template($('#' + defaultID).html());
            }
        },
        initBtn:function () {
            $('#status_code').click(function () {
                $('#navbar_top li:nth-child(3)').click();
            });
        }
    };
})(jQuery, _);